/**
* Projet Chat System
* 
* @author Yann MBOUNGOU et Jean-Christophe VENDOME
* ELEVES INGENIEUR - 4e Année - Informatique et Réseaux
* INSA TOULOUSE, Dept. GEI - 2014 - 2015
*/
package GUI;
import Interfaces.FromUser;
import Interfaces.ToUser;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.UnknownHostException;
import javax.swing.*;

public class MsgPrivate extends JFrame implements ActionListener,FromUser, ToUser{
     final JPanel logPanel,privatePanel; 
     private final JButton retour,psend,pFile;
     private final JTextField pftmessage;
     JScrollPane scroll;
     private final JColorTextPane disp;
     private JFileChooser  fileDialog;
     private File file_joined ;
     private ChatGUI gui;
     private User monu;
    private javax.swing.JButton jButton1;
    private javax.swing.JSeparator jSeparator1;
    private java.awt.Label label1;
    private static int idImage=0;
    
    
    /**
     * README :
    * Cette classe est appelée 
    * à chaque fois qu'on selectionne un utilisateur 
    * connecté de la liste ou lorsque ce dernier nous 
    * envoie un message privé. C'est en effet  
    * une sorte de chat privé comme
    * sur facebook
    */
    
    
     @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
    public  MsgPrivate(User u)
    {
    super("CHAT PRIVATE WITH "+u.getLogin());
    this.monu=u; //<==== on a associé un chat à un seul utilisateur connecté
    privatePanel = new JPanel();
    jSeparator1 = new javax.swing.JSeparator();
    jButton1 = new javax.swing.JButton();
    pftmessage= new JTextField();
    label1 = new java.awt.Label();
    retour = new JButton();
    psend = new JButton();
    pFile = new JButton();
    logPanel = new JPanel();
    disp=new JColorTextPane();   
    scroll=new JScrollPane(getDisp());

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setBackground(new java.awt.Color(0, 204, 204));
    setPreferredSize(new java.awt.Dimension(440, 325));

    privatePanel.setBackground(new java.awt.Color(0, 204, 204));
    idImage++;
    if(idImage>5)
    {
     idImage=1;   
    }
    
    jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Images/icon"+Integer.toString(idImage)+".png"))); // NOI18N
    jButton1.setText("jButton1");

        label1.setFont(new java.awt.Font("Dialog", 2, 12)); // NOI18N
        label1.setForeground(new java.awt.Color(153, 0, 153));
        label1.setText("write here...");

        retour.setForeground(new java.awt.Color(255, 0, 0));
        retour.setText("Go Back");
        retour.addActionListener(this);

        psend.setText("SEND");
        psend.addActionListener(this);

        pFile.setText("File");
        pFile.addActionListener(this);

        javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(privatePanel);
        privatePanel.setLayout(panel1Layout);
        panel1Layout.setHorizontalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addGroup(panel1Layout.createSequentialGroup()
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addComponent(pFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(27, 27, 27)
                                .addComponent(psend, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(25, 25, 25)
                                .addComponent(retour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(pftmessage, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(54, Short.MAX_VALUE))))
        );
        panel1Layout.setVerticalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel1Layout.createSequentialGroup()
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(psend, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(1, 1, 1)
                                .addComponent(pftmessage, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(retour, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(pFile, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE))
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8))
        );

        logPanel.setBackground(new java.awt.Color(0, 204, 204));

        javax.swing.GroupLayout panel2Layout = new javax.swing.GroupLayout(logPanel);
        logPanel.setLayout(panel2Layout);
        panel2Layout.setHorizontalGroup(
            panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(scroll, javax.swing.GroupLayout.PREFERRED_SIZE, 287, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(52, 52, 52))
        );
        panel2Layout.setVerticalGroup(
            panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel2Layout.createSequentialGroup()
                .addComponent(scroll, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(privatePanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(logPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(privatePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(logPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    setVisible(true);// En dernier
  }

    public JColorTextPane getDisp() {
        return disp;
    }
    
    public JTextField getPftmessage() {
        return pftmessage;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        Object obj=e.getSource();
        if (obj instanceof JButton)
        {
           JButton b=(JButton)obj;
           if (b==retour) back(this.monu);
           else if (b==pFile) openPrivate();
           else if (b==psend)try {
               privsend();
           } catch (UnknownHostException ex) {
               System.err.println("!!ChatPriv: Exception ="+ex);
           } 
        }
    }
    
     @Override
    public void openPrivate()
    {
        if (this.getFileDialog() == null) 
        {
            fileDialog = new JFileChooser();    
        }
        int returnVal = this.getFileDialog().showDialog(this,"Join File");
        if (returnVal == JFileChooser.APPROVE_OPTION)
        {
            setFile_joined(this.getFileDialog().getSelectedFile());
            this.getDisp().append(java.awt.Color.black,getFile_joined().getName()+ " joined .\n");
        } 
        else 
        {
            this.getDisp().append(java.awt.Color.red,"Attachment cancelled by "+displayLogin()+".\n");
        }
        this.getDisp().setCaretPosition(this.getDisp().getDocument().getLength());        
        this.getFileDialog().setSelectedFile(null);//Reset the file chooser for the next time it's shown.
    }
    
        public void privsend() throws UnknownHostException                                                                                        
    {
        String mm="";
        if(((this.getFile_joined() == null)==false)&& (this.getPftmessage().getText().equals(mm)==false))
        {
            this.getDisp().append("The file ");
            this.getDisp().append(java.awt.Color.orange,this.getFile_joined().getName());
            this.getDisp().append(" has been joined\n");              
            //***************************************************************//
            this.getDisp().append(java.awt.Color.pink,"You");
            this.getDisp().append(" : "+this.getPftmessage().getText()+"\n"); 
            for(int i=0;i<getGui().getUserList().getSize();i++)
            {    
                String m=""+getGui().getjUserList().getSelectedValue();
                User u = getGui().getUserList().elementAt(i);
                if(u.getLogin().equals(monu.getLogin()))  
                {
                    System.out.println("msg and file private to "+u);
                    getGui().getChatController().performSendprivate(displayLogin(),this.getPftmessage().getText(),u.getAddr());
                    getGui().getChatController().performSendFileprivate(displayLogin(),u.getAddr(),this.getFile_joined());
                }
                else if(u.getLogin().equals(m))  
                {
                    System.out.println("msg and file private to "+u);
                    getGui().getChatController().performSendprivate(displayLogin(),this.getPftmessage().getText(),u.getAddr());
                    getGui().getChatController().performSendFileprivate(displayLogin(),u.getAddr(),this.getFile_joined());
                }
            }
            pftmessage.setText("");
        }
        else if((this.getPftmessage().getText().equals(mm)==false)&&((this.getFile_joined() == null)==true))
        {
            this.getDisp().append(java.awt.Color.pink,"You");
            this.getDisp().append(" : "+this.getPftmessage().getText()+"\n"); 
            for(int i=0;i<getGui().getUserList().getSize();i++)
            {     
                User u = getGui().getUserList().elementAt(i);
                String m=""+getGui().getjUserList().getSelectedValue();
                if(u.getLogin().equals(monu.getLogin()))  
                {
                    System.out.println("msg private to "+u);
                    getGui().getChatController().performSendprivate(displayLogin(),this.getPftmessage().getText(),u.getAddr());
                }
                else if(u.getLogin().equals(m))  
                {
                    System.out.println("msg private to "+u);
                    getGui().getChatController().performSendprivate(displayLogin(),this.getPftmessage().getText(),u.getAddr());
                }
            }
            pftmessage.setText("");
        }
        else if(((this.getFile_joined() == null)==false)&&(this.getPftmessage().getText().equals(mm)==true))
        {
            this.getDisp().append("The file ");
            this.getDisp().append(java.awt.Color.orange,this.getFile_joined().getName());
            this.getDisp().append(" has been joined\n"); 
            for(int i=0;i<getGui().getUserList().getSize();i++)
            {     
                User u = getGui().getUserList().elementAt(i);                   
                String m=""+getGui().getjUserList().getSelectedValue();
                if(u.getLogin().equals(monu.getLogin()))  
                {
                    System.out.println("File private to "+u);
                    getGui().getChatController().performSendFileprivate(displayLogin(),u.getAddr(),this.getFile_joined());
                }
                else if(u.getLogin().equals(m))  
                {
                    System.out.println("File private to "+u);
                    getGui().getChatController().performSendFileprivate(displayLogin(),u.getAddr(),this.getFile_joined());
                }
            }
        }              
    }
        
     public void back(User u)                                                                          
    {  
        if(u.equals(this.monu))
        {
            disp.setText("");
            this.setVisible(false);
            u.setmsgPrive(false);
        }        
    }  

    @Override
    public void connect() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void disconnect() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String displayLogin() {
    return this.getGui().getJftlogin().getText();    
    }

    @Override
    public void send() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void open() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void connected() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void disconnected() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void ReceiveMsg(String login, String message) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
        //*********************File joinned****************************//
    public File getFile_joined() {
        return file_joined;
    }

    public JFileChooser getFileDialog() {
        return fileDialog;
    }

    public void setGui(ChatGUI gui) {
        this.gui = gui;
    }

    public ChatGUI getGui() {
        return gui;
    }

    public User getMonu() {
        return monu;
    }

    public void setMonu(User monu) {
        this.monu = monu;
    }

    public void setFile_joined(File file_joined) {
        this.file_joined = file_joined;
    }
}
